﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGioiThieu.Models;

namespace WebGioiThieu.Areas.Admin.Controllers
{
    public class MenuItemManagerController : Controller
    {
        private IntroductionWebDBEntities db = new IntroductionWebDBEntities();

        //
        // GET: /Admin/MenuItemManager/
        [CustomAuthorize]
        public ViewResult Index()
        {
            var menuitems = db.MenuItems.Include(m => m.MenuType).Include(m => m.Tours);
            return View(menuitems.ToList());
        }

        //
        // GET: /Admin/MenuItemManager/Details/5
        [CustomAuthorize]
        public ViewResult Details(int id)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            return View(menuitem);
        }

        //
        // GET: /Admin/MenuItemManager/Create
        [CustomAuthorize]
        public ActionResult Create()
        {
            ViewBag.MenuTypeID = new SelectList(db.MenuTypes, "ID", "Ten");
            ViewBag.Parent = new SelectList(db.MenuItems, "ID", "Ten", db.MenuItems);
            ViewBag.ID = new SelectList(db.Tours, "ID", "BiDanh");
            return View();
        }

        //
        // POST: /Admin/MenuItemManager/Create
        [CustomAuthorize]
        [HttpPost]
        public ActionResult Create(MenuItem menuitem)
        {
            if (menuitem.BiDanh == null)
                menuitem.BiDanh = menuitem.Ten.Replace(" ", "-");
            if (ModelState.IsValid)
            {
                if (menuitem.Parent == null)
                    menuitem.Parent = 0;
                if (menuitem.ThuTu == null)
                {
                    if (db.MenuItems.Count() == 0)
                        menuitem.ThuTu = 0;
                    else
                        menuitem.ThuTu = db.MenuItems.Count() + 1;
                }
                db.MenuItems.Add(menuitem);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.MenuTypeID = new SelectList(db.MenuTypes, "ID", "Ten", menuitem.MenuTypeID);
            ViewBag.ID = new SelectList(db.Tours, "ID", "BiDanh", menuitem.ID);
            return View(menuitem);
        }

        //
        // GET: /Admin/MenuItemManager/Edit/5
        [CustomAuthorize]
        public ActionResult Edit(int id)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            ViewBag.MenuTypeID = new SelectList(db.MenuTypes, "ID", "Ten", menuitem.MenuTypeID);
            ViewBag.Parent = new SelectList(db.MenuItems, "ID", "Ten",db.MenuItems.Where(i=>i.MenuTypeID==menuitem.MenuTypeID));
            ViewBag.ID = new SelectList(db.Tours, "ID", "BiDanh", menuitem.ID);
            return View(menuitem);
        }

        //
        // POST: /Admin/MenuItemManager/Edit/5
        [CustomAuthorize]
        [HttpPost]
        public ActionResult Edit(MenuItem menuitem)
        {
            if (menuitem.BiDanh == null)
                menuitem.BiDanh = menuitem.Ten.Replace(" ", "-");
            if (ModelState.IsValid)
            {
                if (menuitem.Parent == null)
                    menuitem.Parent = 0;
                db.Entry(menuitem).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MenuTypeID = new SelectList(db.MenuTypes, "ID", "Ten", menuitem.MenuTypeID);
            ViewBag.Parent = new SelectList(db.MenuItems, "ID", "Ten", db.MenuItems.Where(i => i.MenuTypeID == menuitem.MenuTypeID));
            ViewBag.ID = new SelectList(db.Tours, "ID", "BiDanh", menuitem.ID);
            return View(menuitem);
        }

        //
        // GET: /Admin/MenuItemManager/Delete/5
        [CustomAuthorize]
        public ActionResult Delete(int id)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            return View(menuitem);
        }

        //
        // POST: /Admin/MenuItemManager/Delete/5
        [CustomAuthorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(int id, FormCollection Collection)
        {
            MenuItem menuitem = db.MenuItems.Find(id);
            db.MenuItems.Remove(menuitem);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}